//
//  OfferToBuyViewController.m
//  1243
//
//  Created by 洛水寒 on 16/5/24.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "OfferToBuyViewController.h"

#import "NetWorkTools.h"//网络请求

@interface OfferToBuyViewController ()



@property (weak, nonatomic) IBOutlet UITextField *saleNum;//出售数量
@property (weak, nonatomic) IBOutlet UITextField *salePrice;//出售价格
@property (weak, nonatomic) IBOutlet UILabel *totalPrice;//总计金额
@property (weak, nonatomic) IBOutlet UILabel *marketPrice;//市场价

@property (weak, nonatomic) IBOutlet UIImageView *chooseMoney;//选择金额
@property (weak, nonatomic) IBOutlet UIImageView *chooseBonus;//选择积分
@property (weak, nonatomic) IBOutlet UILabel *money;//金额
@property (weak, nonatomic) IBOutlet UILabel *bonus;//积分

@property (strong, nonatomic) NSString *isSelected;

@end

@implementation OfferToBuyViewController
{
    NSNotificationCenter *_nc;//通知中心
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"发布购买";
    
    _isSelected = @"1";
    
    //网络请求
    [self getMoney];
    [self getMarketPrice];
    
    
    _salePrice.keyboardType = UIKeyboardTypeDecimalPad;
    
    
    _nc = [NSNotificationCenter defaultCenter];
    //注册观察者 通知的名字一致
    [_nc addObserver:self selector:@selector(saleNumDidEndEdit) name:UITextFieldTextDidChangeNotification object:nil];
    [_nc addObserver:self selector:@selector(salePricedidEndEdit) name:UITextFieldTextDidChangeNotification object:nil];
}

#pragma mark === 通知方法 ===
- (void)saleNumDidEndEdit{
    if (![_saleNum.text isEqualToString:@""] && ![_salePrice.text isEqualToString:@""]) {
        _totalPrice.text = [NSString stringWithFormat:@"%.2f", [_saleNum.text floatValue] * [_salePrice.text floatValue]];
    }
}
- (void)salePricedidEndEdit{
    if (![_saleNum.text isEqualToString:@""] && ![_salePrice.text isEqualToString:@""]) {
        _totalPrice.text = [NSString stringWithFormat:@"%.2f", [_saleNum.text floatValue] * [_salePrice.text floatValue]];
    }
}


#pragma mark === 网络请求 ===
- (void)getMarketPrice{
    NSDictionary *params = @{
                             };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params AndMenthodName:@"getPriceAndQty" completeBlock:^(id object) {
        
        
        
        NSString *str = object[@"BackData"];
        NSArray *arr = [NetWorkTools arrayWithJsonString:str];
        
        if (arr == nil) {
            _marketPrice.text = @"";
        }else{
            _marketPrice.text = [NSString stringWithFormat:@"%@", arr[0][@"MarketPrice"]];
        }
    }];
}
- (void)getMoney{
    NSDictionary *dict = [ReadLoginData readLoginData];
    NSString *userNumber = [dict objectForKey:@"Number"];
    NSString *UserName = [dict objectForKey:@"UserName"];
    
    NSDictionary *params2 = @{
                              //
                              @"Id":userNumber,
                              @"UserName":UserName
                              };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params2 AndMenthodName:@"getUserInfo" completeBlock:^(id object) {
        
        
        NSString *str = object[@"BackData"];
        NSArray *arr = [NetWorkTools arrayWithJsonString:str];
//        NSLog(@"用户信息%@",object);
        if (arr == nil) {
            self.money.text = @"";
            self.bonus.text = @"";
        }else if ([arr[0][@"Money"] isEqual:[NSNull null]]){
            self.money.text = @"";
        }else if ([arr[0][@"fh_Money"] isEqual:[NSNull null]]){
            self.bonus.text = @"";
        }
        else{
            self.money.text =  [NSString stringWithFormat:@"%.2f",[arr[0][@"Money"] floatValue]];
            self.bonus.text =  [NSString stringWithFormat:@"%.2f",[arr[0][@"fh_Money"] floatValue]];
        }
    }];
}
- (void)getBuyInfo{
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    NSString *TradeUserId = [dict objectForKey:@"Number"];
    NSString *TradeQty = _saleNum.text;
    NSString *TradePrice = _salePrice.text;
    NSString *PayMoney = _totalPrice.text;
    NSString *PayType = _isSelected;
    NSString *MarketPrice = _marketPrice.text;
    
    
    NSDictionary *params = @{
                             //
                             @"TradeUserId":TradeUserId,
                             @"TradeQty":TradeQty,
                             @"TradePrice":TradePrice,
                             @"PayMoney":PayMoney,
                             @"PayType":PayType,
                             @"MarketPrice":MarketPrice
                             };
    
    [[NetWorkTools sharedNetWorkTools] OtherobjectWithURLDict:params AndMenthodName:@"PreBuyTicket" completeBlock:^(id object) {
        
        if ([[NSString stringWithFormat:@"%@",object[@"Id"]] isEqualToString:@"0"]) {
            self.salePrice.text = @"";
            self.saleNum.text = @"";
            self.totalPrice.text=  @"";
            [self getMoney];
        }
        
        //提示框
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:object[@"Message"] message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        NSLog(@"%@", object[@"Message"]);
        [message show];
    }];
}

//金额支付
- (IBAction)payUseMoney:(id)sender {
    _isSelected = @"1";
    _chooseMoney.backgroundColor = [UIColor lightGrayColor];
    _chooseBonus.backgroundColor = [UIColor whiteColor];
}

//积分支付
- (IBAction)payUseBonus:(id)sender {
    _isSelected = @"2";
    _chooseBonus.backgroundColor = [UIColor lightGrayColor];
    _chooseMoney.backgroundColor = [UIColor whiteColor];
}

//确认发布
- (IBAction)offerToBuy:(id)sender {
    //网络请求
    [self getBuyInfo];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_saleNum resignFirstResponder];
    [_salePrice resignFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
